package com.food.user.controller;


import com.food.user.commos.Result;
import com.food.user.exception.MyParameterException;
import com.food.user.form.EsForm;
import com.food.user.model.StoreInfoModel;
import com.food.user.service.StoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 商家信息 前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@RestController
@RequestMapping("/storeInfo")
@Api(tags = "店铺信息接口")
public class StoreInfoController {
    @Autowired
    private StoreInfoService storeInfoService;
    @GetMapping("/list")
    @ApiOperation("es查询全部店铺")
    public Result list(){

        List<StoreInfoModel> storeInfoModels= storeInfoService.esList();
        return Result.success(200,"es查询所有营业店铺成功",storeInfoModels);
    }

    @GetMapping("/detail")
    @ApiOperation("es查询店铺详情")
    public Result detail(Long storeInfoId){

        StoreInfoModel storeInfoModel= storeInfoService.detail(storeInfoId);
        return Result.success(200,"es查询所有营业店铺成功",storeInfoModel);
    }

    @GetMapping("/condition")
    @ApiOperation("es查询店铺详情")
    public Result condition(@Valid EsForm form, BindingResult br){
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }
        List<StoreInfoModel> condition = storeInfoService.condition(form);
        return Result.success(200,"es条件查询所有营业店铺成功",condition);
    }

    @GetMapping("/type")
    @ApiOperation("es类别查询店铺")
    public Result type(Long typeId){
        List<StoreInfoModel> condition = storeInfoService.type(typeId);
        return Result.success(200,"es类别查询所有营业店铺成功",condition);
    }


}

